package com.wisely.ch8_4.service.person;

import com.wisely.ch8_4.domain.Person;
import com.wisely.ch8_4.repository.PersonRepository;
import com.wisely.ch8_4.service.IPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

@Service
public class PersonServiceImpl implements IPersonService {

    @Autowired
    private PersonRepository personRepository;

    @Transactional(rollbackOn = {IllegalArgumentException.class})
    @Override
    public Person savePersonWithRollback(Person person) {
        Person p = personRepository.save(person);
        if(p.getName().equals("Tom")) {
            throw new IllegalArgumentException("Tom is exist,the data will rollback!");
        }
        return p;
    }

    @Transactional(dontRollbackOn = {IllegalArgumentException.class})
    @Override
    public Person savePersonWithoutRollback(Person person) {
        Person p = personRepository.save(person);
        if(p.getName().equals("Tom")) {
            throw new IllegalArgumentException("Tom is exist,the data will not rollback!");
        }
        return p;
    }
}
